package com.symaster.common.entity;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author yinmiao
 * @date 2021-07-24 18:24
 */
public class Page implements Serializable {
    private static final long serialVersionUID = -6849794470754667710L;

    /**
     * 当前页
     */
    private Integer page;
    /**
     * 每页数据数量
     */
    private Integer pageSize;

    public Page(Integer page, Integer pageSize) {
        if (page == null || page <= 1) {
            page = 1;
        }
        if (pageSize == null || pageSize.equals(0)) {
            pageSize = 20;
        }
        this.page = page;
        this.pageSize = pageSize;
    }

    public Page() {
        page = 1;
        pageSize = 20;
    }

    public int[] getStartAndEndIndex(long size) {
        int maxPageNum = (int) Math.ceil(size / pageSize);
        if (this.page >= maxPageNum) {
            this.page = maxPageNum;
        }
        return new int[]{this.page - 1, (this.page - 1) + this.pageSize};
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page page1 = (Page) o;
        return Objects.equals(page, page1.page) && Objects.equals(pageSize, page1.pageSize);
    }

    @Override
    public int hashCode() {
        return Objects.hash(page, pageSize);
    }

    @Override
    public String toString() {
        return "Page{" +
                "page=" + page +
                ", pageSize=" + pageSize +
                '}';
    }
}
